//渲染器
var render = {
	//渲染审核页面Table
	renderVerifyTable : function(target, data){
		if(Array.isArray(data)){
			this.removeChild(target);
			data.forEach(function(item,index,ary){
				var trNode = $("<tr></tr>");
				var td1 = $("<td></td>");
				td1.attr("class","seq");
				td1.html(item.seq);
				trNode.append(td1);
				
				var td2 = $("<td></td>");
				td2.attr("class","name");
				td2.html(item.name);
				trNode.append(td2);
				
				var td3 = $("<td></td>");
				td3.attr("class","store_name");
				td3.html(item.store_name);
				trNode.append(td3);
				
				var td4 = $("<td></td>");
				td4.attr("class","idcard");
				td4.html("<img src='"+item.idcard+"' width='50px'/>");
				trNode.append(td4);
				
				var td5 = $("<td></td>");
				td5.attr("class","img");
				td5.html("<img src='"+item.img+"' width='50px'/>");
				trNode.append(td5);
				
				var td6 = $("<td></td>");
				td6.attr("class","address");
				td6.html(item.address);
				trNode.append(td6);
				
				var td7 = $("<td></td>");
				td7.attr("class","submit_time");
				td7.html(render.formatDate(item.submit_time));
				trNode.append(td7);
				
				var td8 = $("<td></td>");
				td8.attr("class","operation");
				td8.append("<button type='button' class='btn btn-success btn-pass'>Pass</button><button type='button' class='btn btn-danger btn-refuse'>Refuse</button>");
				trNode.append(td8);
				
				var td9 = $("<td></td>");
				td9.attr("class","id");
				td9.css("display","none");
				td9.html(item.id);
				trNode.append(td9);
				$(target).append(trNode);
			});
			//为按钮添加事件
			render.addEvent($(".btn-pass"), "click", function(){
				let id = $(this).parent().parent().children(".id").html();
				let seq = $(this).parent().parent().children(".seq").html();
				
				$.post("/aeat/sec/verify",{"mid":id, "id": seq, "type":"pass"},function(data){
					var obj = JSON.parse(data);
					if(obj.code == 200){
						alert("Pass success");
						window.location.reload();
					}else{
						alert("Pass failure, Error="+obj.msg);
					}
				});
			});
			render.addEvent($(".btn-refuse"), "click", function(){
				let id = $(this).parent().parent().children(".id").html();
				let seq = $(this).parent().parent().children(".seq").html();
				$("#id").val(seq);
				$("#mid").val(id);
				$("#modal-refuse").modal("show");
			});
			render.addEvent($("#modal-refuse-submit"),"click",function(){
				let id = $("#id").val();
				let mid = $("#mid").val();
				let reason = $("#reason").val();
				$.post("/aeat/sec/verify",{"mid":mid, "id": id,"reason":reason , "type":"refuse"},function(data){
					var obj = JSON.parse(data);
					if(obj.code == 200){
						alert("Refuse success");
						$("#id").val("");
						$("#mid").val("");
						$("#modal-refuse").modal("hide");
						window.location.reload();
					}else{
						alert("Refuse failure, Error="+obj.msg);
					}
				});
			});
			//为图片绑定放大器
			render.addEvent($(".idcard"),"click",function(){
				var imgNode = $(this).children("img")[0];
				var src = $(imgNode).attr("src");
				$("#modal-img-target").attr("src", src);
				$("#modal-img").modal("show");
			});
			render.addEvent($(".img"),"click",function(){
				var imgNode = $(this).children("img")[0];
				var src = $(imgNode).attr("src");
				$("#modal-img-target").attr("src", src);
				$("#modal-img").modal("show");
			});
		};
	},
	//渲染待审核商家列表
	renderUntreatedMerchantList : function(target, data){
		if(Array.isArray(data)){
			this.removeChild(target);
			data.forEach(function(item,index,ary){
				var trNode = $("<tr></tr>");
				var td1 = $("<td></td>");
				td1.attr("class","seq");
				td1.html(item.seq);
				trNode.append(td1);
				
				var td2 = $("<td></td>");
				td2.attr("class","name");
				td2.html(item.name);
				trNode.append(td2);
				
				var td3 = $("<td></td>");
				td3.attr("class","store_name");
				td3.html(item.store_name);
				trNode.append(td3);
				
				var td4 = $("<td></td>");
				td4.attr("class","idcard");
				td4.html("<img src='"+item.idcard+"' width='50px'/>");
				trNode.append(td4);
				
				var td5 = $("<td></td>");
				td5.attr("class","img");
				td5.html("<img src='"+item.img+"' width='50px'/>");
				trNode.append(td5);
				
				var td6 = $("<td></td>");
				td6.attr("class","address");
				td6.html(item.address);
				trNode.append(td6);
				
				var td7 = $("<td></td>");
				td7.attr("class","state");
				td7.html(item.state);
				trNode.append(td7);
				
				var td8 = $("<td></td>");
				td8.attr("class","submit_time");
				td8.html(render.formatDate(item.submit_time));
				trNode.append(td8);

				var td9 = $("<td></td>");
				td9.attr("class","id");
				td9.css("display","none");
				td9.html(item.id);
				trNode.append(td9);
				$(target).append(trNode);
			});
			//为图片绑定放大器
			render.addEvent($(".idcard"),"click",function(){
				var imgNode = $(this).children("img")[0];
				var src = $(imgNode).attr("src");
				$("#modal-img-target").attr("src", src);
				$("#modal-img").modal("show");
			});
			render.addEvent($(".img"),"click",function(){
				var imgNode = $(this).children("img")[0];
				var src = $(imgNode).attr("src");
				$("#modal-img-target").attr("src", src);
				$("#modal-img").modal("show");
			});
		};
	},
	//渲染审核通过商家列表
	renderPassMerchantList : function(target, data){
		if(Array.isArray(data)){
			this.removeChild(target);
			data.forEach(function(item,index,ary){
				var trNode = $("<tr></tr>");
				var td1 = $("<td></td>");
				td1.attr("class","seq");
				td1.html(item.seq);
				trNode.append(td1);
				
				var td2 = $("<td></td>");
				td2.attr("class","name");
				td2.html(item.name);
				trNode.append(td2);
				
				var td3 = $("<td></td>");
				td3.attr("class","store_name");
				td3.html(item.store_name);
				trNode.append(td3);
				
				var td4 = $("<td></td>");
				td4.attr("class","idcard");
				td4.html("<img src='"+item.idcard+"' width='50px'/>");
				trNode.append(td4);
				
				var td5 = $("<td></td>");
				td5.attr("class","img");
				td5.html("<img src='"+item.img+"' width='50px'/>");
				trNode.append(td5);
				
				var td6 = $("<td></td>");
				td6.attr("class","address");
				td6.html(item.address);
				trNode.append(td6);
				
				var td7 = $("<td></td>");
				td7.attr("class","state");
				td7.html(item.state);
				trNode.append(td7);
				
				var td8 = $("<td></td>");
				td8.attr("class","submit_time");
				td8.html(render.formatDate(item.submit_time));
				trNode.append(td8);

				var td9 = $("<td></td>");
				td9.attr("class","block");
				var td9Text = "";
				if(item.block == 0){
					td9Text = "<button type='button' class='btn btn-danger btn-block' mid='"+item.id+"' block='1' >Black</button>";
				}else{
					td9Text = "<button type='button' class='btn btn-primary btn-block' mid='"+item.id+"' block='0' >White</button>";
				}
				td9.html(td9Text);
				trNode.append(td9);
				
				var td10 = $("<td></td>");
				td10.attr("class","id");
				td10.css("display","none");
				td10.html(item.id);
				trNode.append(td10);
				$(target).append(trNode);
			});
			//为按钮添加事件
			render.addEvent($(".btn-block"), "click", function(){
				let mid = $(this).attr("mid");
				let block = $(this).attr("block");
				var $this = $(this);
				$.get("/aeat/sec/muser/block?mid="+mid+"&block="+block,function(data){
					var obj = JSON.parse(data);
					if(obj.code == 200){
						alert("Block success");
						if(block==0){
							$($this).html("Black");
							$($this).attr("block","1");
							$($this).attr("class", "btn btn-danger btn-block");
						}else{
							$($this).html("White");
							$($this).attr("block","0");
							$($this).attr("class", "btn btn-primary btn-block");
						}
					}else{
						alert("Block failure, Error="+obj.msg);
					}
				});
			});
			//为图片绑定放大器
			render.addEvent($(".idcard"),"click",function(){
				var imgNode = $(this).children("img")[0];
				var src = $(imgNode).attr("src");
				$("#modal-img-target").attr("src", src);
				$("#modal-img").modal("show");
			});
			render.addEvent($(".img"),"click",function(){
				var imgNode = $(this).children("img")[0];
				var src = $(imgNode).attr("src");
				$("#modal-img-target").attr("src", src);
				$("#modal-img").modal("show");
			});
		};
	},
	//渲染驳回商家列表
	renderRefuseMerchantList : function(target, data){
		if(Array.isArray(data)){
			this.removeChild(target);
			data.forEach(function(item,index,ary){
				var trNode = $("<tr></tr>");
				var td1 = $("<td></td>");
				td1.attr("class","seq");
				td1.html(item.seq);
				trNode.append(td1);
				
				var td2 = $("<td></td>");
				td2.attr("class","name");
				td2.html(item.name);
				trNode.append(td2);
				
				var td3 = $("<td></td>");
				td3.attr("class","store_name");
				td3.html(item.store_name);
				trNode.append(td3);
				
				var td4 = $("<td></td>");
				td4.attr("class","idcard");
				td4.html("<img src='"+item.idcard+"' width='50px'/>");
				trNode.append(td4);
				
				var td5 = $("<td></td>");
				td5.attr("class","img");
				td5.html("<img src='"+item.img+"' width='50px'/>");
				trNode.append(td5);
				
				var td6 = $("<td></td>");
				td6.attr("class","address");
				td6.html(item.address);
				trNode.append(td6);
				
				var td7 = $("<td></td>");
				td7.attr("class","state");
				td7.html(item.state);
				trNode.append(td7);
				
				var td8 = $("<td></td>");
				td8.attr("class","submit_time");
				td8.html(render.formatDate(item.submit_time));
				trNode.append(td8);

				var td9 = $("<td></td>");
				td9.attr("class","verify_time");
				td9.html(render.formatDate(item.verify_time));
				trNode.append(td9);
				
				var td10 = $("<td></td>");
				td10.attr("class","msg");
				td10.html(item.msg);
				trNode.append(td10);
				
				var td11 = $("<td></td>");
				td11.attr("class","id");
				td11.css("display","none");
				td11.html(item.id);
				trNode.append(td11);
				$(target).append(trNode);
			});
			//为图片绑定放大器
			render.addEvent($(".idcard"),"click",function(){
				var imgNode = $(this).children("img")[0];
				var src = $(imgNode).attr("src");
				$("#modal-img-target").attr("src", src);
				$("#modal-img").modal("show");
			});
			render.addEvent($(".img"),"click",function(){
				var imgNode = $(this).children("img")[0];
				var src = $(imgNode).attr("src");
				$("#modal-img-target").attr("src", src);
				$("#modal-img").modal("show");
			});
		};
	},
	
	removeChild : function(target){		 
		$(target).children().remove();
	},
	
	addEvent : function(target, event, fn){
		$(target).bind(event,fn);
	},
	
	formatDate : function(time){
		var now = new Date(time);     
		var year=now.getFullYear();     
        var month=now.getMonth()+1;     
        var date=now.getDate();     
        var hour=now.getHours();     
        var minute=now.getMinutes();     
        var second=now.getSeconds();     
        return year+"-"+month+"-"+date+" "+hour+":"+minute+":"+second;   
	}
};